#!/bin/sh

if [ "$MU_IP" = "" ] && [ "$#" -lt 2 ] || [ "$#" -lt 1 ] || [ $1 = "help" ]; then
  echo 1>&2 "Usage: $0 <action> <ipaddress>
  where action is one of:
    archive <uuid>
    archive_get <jobId>
    archive_status <jobId>
    backup
    backup_status
    backup_get
    capture <port>
    capture_get <jobId>
    delete <uuid>
    delete_template <uuid>
    export <uuid>
    export_byname <name>
    faults <uuid>
    import <file.xml> 
    jobs
    job_cancel <jobId>
    job_status <jobId>
    list analysis|templates
    list_if aborted|failed|finished|new|queued|running|suspended
    list_type <template-type>
    find <type>|'<type>&filter=<filter>'
    pause <uuid>
    resume <uuid>
    run <uuid>
    stop <uuid>
    status <uuid>
    types

  "
  echo 1>&2 "You may set a permanant IP address: export MU_IP=<ip address>"
  echo 1>&2 "Or include an ip address with each command"
  echo 1>&2 "You must also export user and password: export MU_ADMIN=<admin>; export MU_PASSWORD=<password>"
  exit 127
fi

export command=$1
export value=$2

if [ "$#" == 3 ]; then
  export MU_IP=$3
fi

export MU_ADMIN="admin"

if [ "$MU_PASSWORD" = "" ]; then
  export MU_PASSWORD="admin"
fi

#echo "command: $command"
#echo "value: $value"
#echo "ip set to: $MU_IP"

case $command in
  archive)
    curl -k -G --user $MU_ADMIN:$MU_PASSWORD --data "uuid=${value}" https://$MU_IP/api/v3/archive/run ;;
  archive_status)
    curl -k -G --user $MU_ADMIN:$MU_PASSWORD --data "jobId=${value}" https://$MU_IP/api/v3/archive/status ;;
  archive_get)
    curl -k -G --user $MU_ADMIN:$MU_PASSWORD --data "jobId=${value}" https://$MU_IP/api/v3/archive/get >${value}.zip ;;
  jobs)
    curl -k -G --user $MU_ADMIN:$MU_PASSWORD https://$MU_IP/api/v3/jobs/list ;;
  backup)
    curl -k -G --user $MU_ADMIN:$MU_PASSWORD https://$MU_IP/api/v3/backup/run ;;
  backup_status)
    curl -k -G --user $MU_ADMIN:$MU_PASSWORD https://$MU_IP/api/v3/backup/status ;;
  backup_get)
    curl -k -G --user $MU_ADMIN:$MU_PASSWORD https://$MU_IP/api/v3/backup/get >${value}.zip ;;
  capture)
    curl -k -G --user $MU_ADMIN:$MU_PASSWORD --data "port=${value}" https://$MU_IP/api/v3/capture/run ;;
  capture_get)
    curl -k -G --user $MU_ADMIN:$MU_PASSWORD --data "jobId=${value}" https://$MU_IP/api/v3/capture/get >${value}.pcap ;;
  job_cancel)
    curl -k -G --user $MU_ADMIN:$MU_PASSWORD --data "jobId=${value}" https://$MU_IP/api/v3/jobs/cancel ;;
  job_status)
    curl -k -G --user $MU_ADMIN:$MU_PASSWORD --data "jobId=${value}" https://$MU_IP/api/v3/jobs/status ;;
  export_byname)
    curl -k -G --user $MU_ADMIN:$MU_PASSWORD --data "type=analysis&name=${value}" https://$MU_IP/api/v3/templates/export ;;
  export|get)
    curl -k -G --user $MU_ADMIN:$MU_PASSWORD --data "uuid=${value}" https://$MU_IP/api/v3/templates/export ;;
  import|post)
    curl -k --user $MU_ADMIN:$MU_PASSWORD --data @${value} https://$MU_IP/api/v3/templates/import ;;
  types)
    curl -k --user $MU_ADMIN:$MU_PASSWORD https://$MU_IP/api/v3/templates/types ;;
  delete_template)
    curl -k -G --user $MU_ADMIN:$MU_PASSWORD --data "uuid=${value}" https://$MU_IP/api/v3/templates/delete ;;
  list)
    curl -k -G --user $MU_ADMIN:$MU_PASSWORD https://$MU_IP/api/v3/${value}/list ;;
  list_if)
    curl -k -G --user $MU_ADMIN:$MU_PASSWORD https://$MU_IP/api/v3/analysis/list?status=${value} ;;
  list_type)
    curl -k -G --user $MU_ADMIN:$MU_PASSWORD https://$MU_IP/api/v3/templates/list?type=${value} ;;
  find)
    curl -k -G --user $MU_ADMIN:$MU_PASSWORD https://$MU_IP/rai/rest/mu/findUsing?type=${value} ;;
  run|stop|pause|resume|status|delete)
    curl -k -G --user $MU_ADMIN:$MU_PASSWORD --data "uuid=${value}" https://$MU_IP/api/v3/analysis/${command} ;;
  faults)
    curl -k -G --user $MU_ADMIN:$MU_PASSWORD --data "uuid=${value}" https://$MU_IP/api/v3/analysis/getFaultList ;;
  *)
    echo 1>&2 "unknown command - try $0 with no action for help"
    exit 127
    ;;
esac
